﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Npgsql;
using System.Collections;
using System.Text;
using System.Globalization;
using NpgsqlTypes;
using Reimers.Map;
using Reimers.Map.Controls;
using GraphGear;
using System.Xml;
using System.Collections.Generic;
using System.Web.Configuration;
using Reimers.WebControls;

namespace TeamProject.pics
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {

            base.OnInit(e);

            GMap.ExternalCallback += new Reimers.Map.ExternalCallbackHandler(googleMap_ExternalCallback);

        }

        protected override void OnLoad(EventArgs e)
        {

            base.OnLoad(e);

            Button1.OnClientClick = GMap.CreateMapCallback("A", false) + "return false;";

        }



        void googleMap_ExternalCallback(string Argument, ref string MapCommand)
        {

            //MapCommand = GMap.ClearOverlays();
                        
            switch (Argument)
            {

                case "A":

                    Search(ref MapCommand);
                    break;
                                   
                default:

                    break;

            }

            //MapCommand += GMap.AddOverlay(mrk);//Not persisted to map state, i.e. lost on postback

            //MapCommand += googleMap.AddOverlay(mrk, true); //Persisted to map state, i.e. remembered through postbacks

        }

        protected void Reload(object sender, EventArgs e)
        {
            //Label4.Text = "111searcdddddh";
           // GMap.g.GetBounds();
            
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            //  GMap.GoogleKey = ConfigurationManager.AppSettings["MapKey"];
            //No need to add controls with every postback, they are kept in the viewstate.
            if (!Page.IsPostBack)
            {
                GMap.Center = new Reimers.Map.GoogleLatLng(39, 15);
                GMap.Zoom = 5;
                
                GMap.MapControls.Add(new Reimers.Map.Controls.GoogleLargeMapControl("lmc"));
                GMap.MapControls.Add(new Reimers.Map.Controls.GoogleSmallMapTypeControl("smtc", new Reimers.Map.Controls.GoogleControlPosition(Reimers.Map.GoogleControlAnchor.G_ANCHOR_BOTTOM_RIGHT, new Reimers.Map.GoogleSize(50, 50))));
                GMap.PostRenderScript = GMap.CenterAndZoom(new Reimers.Map.GoogleLatLng(36, 14), 8);
                            }

            List<Node> nodes = new List<Node>();
            nodes.Add(new Node(1, "Tulenisko"));
            nodes.Add(new Node(3, "Marko"));
            nodes.Add(new Node(4, "Gabo"));
            nodes.Add(new Node(12, "Cecil"));

            List<Edge> edges = new List<Edge>();
            edges.Add(new Edge(1, 3));
            edges.Add(new Edge(1, 4));
            edges.Add(new Edge(1, 12));

            Utils.XMLgen(nodes, edges);

        }



        protected void Search(ref string MapCommand)
        {
//            Label4.Text = "search";

            String name = TBSearchedTerm.Text.Substring(0, TBSearchedTerm.Text.IndexOf("   ("));

            if(TBSearchedTerm.Text.Contains("objekt"))
            {

            using (NpgsqlConnection conn = new NpgsqlConnection("database=postgres;server=localhost;port=5432;UID=bassy;password=H2laF3zG"))
            {
                conn.Open();
                String sql3 = "SELECT geo_data, city, street, zip, url, (SELECT type FROM category WHERE category.id = geo_object.category_id)  FROM geo_object WHERE name LIKE '" + name + "'";
                NpgsqlCommand cmd3 = new NpgsqlCommand();
                cmd3.Connection = conn;
                cmd3.CommandText = sql3;
                NpgsqlDataReader dr;

                dr = cmd3.ExecuteReader();
                if (dr.Read())
                {
                    NpgsqlPoint point = (NpgsqlPoint)dr[0];
                    String city = (string)dr[1];
                    String street = (string)dr[2];
                    String zip = (string)dr[3];
                    String url = (string)dr[4];
                    //String categ = (string)dr[5];
                    String categ = "";

                    GoogleMarker mrk = new GoogleMarker(name);
                    mrk.Point = new GoogleLatLng(point.X, point.Y);
                    
                    MapCommand = mrk.Options.Title = name;
                    MapCommand = mrk.MarkerText = name;

                    String htmlString = @"<html><h2>" + name + "</h2><br><br>URL: <a href =\"" + url +"\">" + url + "</a><br><br>" + street + ", " + city + ", " +zip + "<br><br>" + categ + "</html>";
                   
                    mrk.ClientSideHandlers.OnClick = mrk.OpenInfoWindowHTML(GMap, htmlString);
                    MapCommand = GMap.CenterAndZoom(new Reimers.Map.GoogleLatLng(point.X, point.Y), 15);
                    MapCommand += GMap.AddOverlay(mrk,true);
                    
                }
                
               
            }
            }
            else if (TBSearchedTerm.Text.Contains("mesto") || TBSearchedTerm.Text.Contains("ulica"))
            {
                Reimers.Map.Geocoding.Address addr = new Reimers.Map.Geocoding.Address(name, "SK");
                Reimers.Map.Geocoding.GoogleResult result = Reimers.Map.Geocoding.GoogleGeocoder.Geocode(addr, null);
                Reimers.Map.GoogleMarker marker = new GoogleMarker(name);
                marker.Point = new GoogleLatLng(result.Locations[0].Point.Latitude, result.Locations[0].Point.Longitude);
                MapCommand += marker.OpenInfoWindowHTML(GMap, string.Format("Lat: {0}<br />Lng: {1}<br />Address: {2}",
                    result.Locations[0].Point.Latitude,
                    result.Locations[0].Point.Longitude,
                    result.Locations[0].Address));

                MapCommand = GMap.CenterAndZoom(new Reimers.Map.GoogleLatLng(result.Locations[0].Point.Latitude, result.Locations[0].Point.Longitude), 15);
                MapCommand += GMap.AddOverlay(marker, true);
            }
            else if (TBSearchedTerm.Text.Contains("koncept"))
            {
               
                    using (NpgsqlConnection conn = new NpgsqlConnection("database=postgres;server=localhost;port=5432;UID=bassy;password=H2laF3zG"))
                    {
                        conn.Open();
                        String sql3 = "SELECT geo_data, city, street, zip, url, (SELECT type FROM category WHERE category.id = geo_object.category_id),name  FROM geo_object, category WHERE category.type LIKE '" + name + "' LIMIT 200";
                        NpgsqlCommand cmd3 = new NpgsqlCommand();
                        cmd3.Connection = conn;
                        cmd3.CommandText = sql3;
                        NpgsqlDataReader dr;

                        dr = cmd3.ExecuteReader();
                        int i = 1;

                        while (dr.Read())
                        {
                            i++;
                            NpgsqlPoint point = (NpgsqlPoint)dr[0];
                            String city = (string)dr[1];
                            String street = (string)dr[2];
                            String zip = (string)dr[3];
                            String url = (string)dr[4];
                            //String categ = (string)dr[5];
                            String categ = "";
                            String nm = (string)dr[6];

                            GoogleMarker mrk = new GoogleMarker(i + " ");
                            mrk.Point = new  GoogleLatLng(point.X, point.Y);

                            mrk.Options.Title = name;
                            mrk.MarkerText = name;

                            String htmlString = @"<html><h2>" + name + "</h2><br><br>URL: <a href =\"" + url + "\">" + url + "</a><br><br>" + street + ", " + city + ", " + zip + "<br><br>" + categ + "</html>";

                            mrk.ClientSideHandlers.OnClick = mrk.OpenInfoWindowHTML(GMap, htmlString);
                            MapCommand += GMap.AddOverlay(mrk, true);


                            
                        }
                        
                        MapCommand += GMap.CenterAndZoom(new Reimers.Map.GoogleLatLng(47, 18), 8);
                    
                }
            }
        }
             

        protected void CancelPressed(object sender, EventArgs e)
        {
            TBEmail.Text = "assssssssssssssssss"; ModalPopupExtender1.Hide();
            Label2.Text = "assssssssssssssssssssssss";
        }

        protected void Register(object sender, EventArgs e)
        {
            // TBEmail.Text = "assssssssssssssssss";
            //  PanelRegistration.Visible = false;
            ModalPopupExtender1.Hide();
        }


        protected void Login(object sender, EventArgs e)
        {
           // Label4.Text = "asdfghjk";
        }


    }
}
